package com.shop.service.impl;

import com.shop.entity.SysPerssion;
import com.shop.entity.SysUser;
import com.shop.mapper.SysPerssionMapper;
import com.shop.mapper.SysRolePerssionMapper;
import com.shop.service.SysPerssionService;
import com.shop.utils.PageUtils;
import com.shop.utils.ResultUtils;
import com.shop.utils.StringEmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysPerssionServiceImpl implements SysPerssionService {
    @Autowired
    private SysPerssionMapper sysPerssionMapper;
    @Autowired
    SysRolePerssionMapper sysRolePerssionMapper;
    @Override
    public ResultUtils findPerssionResouce(Integer current, Integer size) {
        Map<String,Object> pageParams=new HashMap<>();
        if (current == null) {
            current = 1;
        }
        if (size == null ) {
            size = 10;
        }
        pageParams.put("current",current);
        pageParams.put("size",size);
        List<SysPerssion> userRoleByNameOrAll = sysPerssionMapper.findSysPerssion(pageParams);
        Integer userRoleByNameOrAllCount =   sysPerssionMapper.findSysPerssionCount(pageParams);
        return  PageUtils.pageInfo(userRoleByNameOrAll,userRoleByNameOrAllCount,current,size);
    }

    @Override
    public ResultUtils savePerssionResouce(SysPerssion sysPerssion) {
         if(!StringEmptyUtils.isEmpty(sysPerssion.getName())){
             return  ResultUtils.error(500,"资源名称不能为空");
         }
        if(!StringEmptyUtils.isEmpty(sysPerssion.getPerssion())){
            return  ResultUtils.error(500,"权限不能为空");
        }
        if(!StringEmptyUtils.isEmpty(sysPerssion.getParentId())){
            return  ResultUtils.error(500,"父级id不能为空");
        }
        if(!StringEmptyUtils.isEmpty(sysPerssion.getSort())){
            return  ResultUtils.error(500,"排序不能为空");
        }
        sysPerssion.setCreateDate(new Date());
        sysPerssion.setUpdateDate(new Date());
        int insert = sysPerssionMapper.insert(sysPerssion);

        if(insert>0){
            return ResultUtils.ok();
        } else {
            return  ResultUtils.error(500,"新增失败");
        }
    }

    @Override
    public ResultUtils deltePerssionResouce(Integer id) {
        int sysPerssion = sysPerssionMapper.deleteByPrimaryKey(id);
        Integer sysRolePerssion = sysRolePerssionMapper.deleteSysPerssionByPerssionId(id);
        if(sysPerssion>0||sysRolePerssion>0){
            return ResultUtils.ok();
        }
        return ResultUtils.error(500,"删除失败");
    }

    @Override
    public ResultUtils updatePerssionResouce(SysPerssion sysPerssion) {
        int byPrimaryKey = sysPerssionMapper.updateByPrimaryKeySelective(sysPerssion);
        if(byPrimaryKey>0){
            return ResultUtils.ok();
        }
        return ResultUtils.error(500,"修改失败");    }

    @Override
    public ResultUtils findPerssionResouceByRoleId(List<Integer> id) {
        return ResultUtils.ok(sysPerssionMapper.findPerssionResouceByRoleId(id));
    }
}
